package zmee.pmas.form;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.EventObject;

/**
 * @author yss
 * @createTime 2022-11-30 15:15
 * @description 项目支出预算单据扩展
 */
public class OutBudgetBillPluginEx extends AbstractBillPlugIn {

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        String billstatus = (String) this.getModel().getValue("billstatus");
        if("A".equals(billstatus)){
            DynamicObject projectDy = (DynamicObject) this.getModel().getValue("project");
            Date yearDate = (Date) getModel().getValue("year");
            propertyChanged_project(projectDy,yearDate);
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        IDataEntityProperty property = e.getProperty();
        String name = property.getName();
        ChangeData changeData = e.getChangeSet()[0];
        Object newValue = changeData.getNewValue();
        Object oldValue = changeData.getOldValue();
        if("project".equals(name)){
            propertyChanged_project((DynamicObject) newValue, (Date) getModel().getValue("year"));
        }else if("year".equals(name)){
            DynamicObject projectDy = (DynamicObject) this.getModel().getValue("project");
            propertyChanged_project(projectDy, (Date) newValue);
        }
    }

    private void propertyChanged_project(DynamicObject projectDy,Date yearDate){
        /*一、项目概算总额  来源逻辑：项目立项单 项目投资预算总额/项目概算（元） 字段*/
        BigDecimal projcetBugAmt = BigDecimal.ZERO;
        String budgetyear = "";
        if(projectDy != null && yearDate != null){
            long projectPk = projectDy.getLong("id");
            //1.2 根据项目获取项目立项信息
            DynamicObject proApprovalDy = BusinessDataServiceHelper.loadSingleFromCache("pmas_pro_approval", "projcetbugamt", new QFilter[]{new QFilter("pro.id", QCP.equals,projectPk)});
            projcetBugAmt = proApprovalDy.getBigDecimal("projcetbugamt");//项目概算总额

            /*二、年度财务预算   来源逻辑：取值EAS年度财务预算*/
            //1、获取年度信息
            Date year = (Date) this.getModel().getValue("year");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(year);
            int yearInt = calendar.get(Calendar.YEAR);
            Date yearFirst = getYearFirst(yearInt);
            Date yearLast = getYearLast(yearInt);

            QFilter projectBudgetQFilter = new QFilter("projectname.id",QCP.equals,projectPk);
            projectBudgetQFilter.and(new QFilter("date",QCP.large_equals,yearFirst).and(new QFilter("date", QCP.less_equals,yearLast)));
            DynamicObject projectBudgetDy = BusinessDataServiceHelper.loadSingleFromCache("zmee_project_budget", "budgetyear", projectBudgetQFilter.toArray());
            if(projectBudgetDy != null){
                budgetyear = projectBudgetDy.getString("budgetyear");
            }
        }
        this.getModel().setValue("zmee_textfield",projcetBugAmt);
        this.getModel().setValue("zmee_textf",budgetyear);
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public Date getYearFirst(int year)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public Date getYearLast(int year)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, - 1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }
}
